<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图书列表展示</title>
    <link rel="stylesheet" href="css/bootstrap.min.css">
    <link rel="stylesheet" href="css/list.css">
    <script src="js/jquery.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/jq-paginator.js"></script>
</head>
<body>
<div class="bookContainer">
    <h2>图书列表展示</h2>
    <div class="navbar-justify-between">
        <div>
            <button class="btn btn-outline-info" type="button" onclick="location.href='book_add.html'">添加图书</button>
            <button class="btn btn-outline-info" type="button" onclick="batchDelete()">批量删除</button>
        </div>
    </div>

    <table class="table">
        <thead>
        <tr>
            <th>选择</th>
            <th class="width100">图书ID</th>
            <th>书名</th>
            <th>作者</th>
            <th>数量</th>
            <th>定价</th>
            <th>出版社</th>
            <th>状态</th>
            <th class="width200">操作</th>
        </tr>
        </thead>
        <tbody id="bookList">
        <!-- 静态示例数据 -->
<!--        <tr>-->
<!--            <td>-->
<!--                <input type="checkbox" name="selectBook" value="1" id="selectBook_1" class="book-select">-->
<!--            </td>-->
<!--            <td>2</td>-->
<!--            <td>大秦帝国第二册</td>-->
<!--            <td>我是作者</td>-->
<!--            <td>23</td>-->
<!--            <td>33.00</td>-->
<!--            <td>北京出版社</td>-->
<!--            <td>可借阅</td>-->
<!--            <td>-->
<!--                <div class="op">-->
<!--                    <a href="book_update.html?bookId=2">修改</a>-->
<!--                    <a href="javascript:void(0)" onclick="deleteBook(2)">删除</a>-->
<!--                </div>-->
<!--            </td>-->
<!--        </tr>-->
        </tbody>
    </table>

    <div class="demo">
        <ul id="pageContainer" class="pagination justify-content-center"></ul>
    </div>

    <script>
        getBookList();

        function getBookList() {
            $.ajax({
                type: "get",
                url: "book/getListByPage"+location.search,//location.search得到url中的参数
                success: function (result) {
                    console.log(result);

                    var data=result.data;
                    var books=data.records;
                    var finalHtml = "";
                    // 生成数据列表
                    for (var book of books) {
                        finalHtml += "<tr>";
                        finalHtml += '<td><input type="checkbox" name="selectBook" value="' + book.id + '" id="selectBook_' + book.id + '" class="book-select"/></td>';
                        finalHtml += "<td>" + book.id + "</td>";
                        finalHtml += "<td>" + book.bookName + "</td>";
                        finalHtml += "<td>" + book.author + "</td>";
                        finalHtml += "<td>" + book.count + "</td>";
                        finalHtml += "<td>" + book.price + "</td>";
                        finalHtml += "<td>" + book.publish + "</td>";
                        finalHtml += "<td>" + book.statusCN + "</td>";
                        finalHtml += "<td>";
                        finalHtml += '<div class="op">';
                        finalHtml += '<a href="book_update.html?bookId=' + book.id + '">修改</a>';
                        finalHtml += ' <a href="javascript:void(0)" onclick="deleteBook(' + book.id + ')">删除</a>';
                        finalHtml += "</div>";
                        finalHtml += "</td>";
                        finalHtml += "</tr>";
                    }
                    $("#bookList").html(finalHtml);
                    //非空检验省略
                    // var currentPage=result.pageRequest.currentPage
                        var request=result.data.currentPage;
                    // 翻页配置
                    $("#pageContainer").jqPaginator({
                        totalCounts: data.total,    // 总记录数
                        pageSize: 10,        // 每页个数
                        visiblePages: 5,     // 可视页数
                        currentPage: data.pageRequest.currentPage,      // 当前页码
                        first: '<li class="page-item"><a class="page-link" href="javascript:void(0);">首页</a></li>',
                        prev: '<li class="page-item"><a class="page-link" href="javascript:void(0);">上一页</a></li>',
                        next: '<li class="page-item"><a class="page-link" href="javascript:void(0);">下一页</a></li>',
                        last: '<li class="page-item"><a class="page-link" href="javascript:void(0);">最后一页</a></li>',
                        page: '<li class="page-item"><a class="page-link" href="javascript:void(0);">{{page}}</a></li>',
                        onPageChange: function (page, type) {
                            console.log("第" + page + "页, 类型:" + type);
                            // 如有需要，可在这里调用 getBookList() 请求对应页数据
                            if(type=="change"){
                                location.href="book_list.html?currentPage="+page;
                            }
                        }
                    });
                },
                //如果没有登陆后端设置401，ajax请求是不会成功的会到error这里
                error: function (error) {
                    if (error.status == 401) {
                        //用户未登录
                        location.href="login.html";
                    }
                }

            });
        }


        function deleteBook(id) {
            var isDelete = confirm("确认删除?");
            if (isDelete) {
                $.ajax({
                    type:"post",
                    url:"/book/deleteBook",
                    data:{
                        bookId:id,
                    },
                    success:function(result){
                        if(result!=null
                        &&result.code==200
                        &&result.data==""){
                            location.href="book_list.html";
                        }else{
                            //删除失败
                            alert(result);
                        }
                    }
                })
                // 调用删除接口
                alert("删除成功");
            }
        }

        function batchDelete() {
            var isDelete = confirm("确认批量删除?");
            if (isDelete) {
                // 获取所有选中的复选框的值
                var ids = [];
                $("input:checkbox[name='selectBook']:checked").each(function () {
                    ids.push($(this).val());
                });
                // console.log(ids);
                $.ajax({
                    type:"post",
                    url:"/book/batchDeleteBook?ids="+ids,
                    success: function (result){
                        console.error(result);
                        if(result!=null
                            &&result.code==200
                            &&result.data==""){
                            alert("批量删除成功");
                            location.href="book_list.html";
                        }else{
                            console.error("批量删除失败，请连续管理员.");
                        }
                    }
                })
            }
        }
    </script>
</div>
</body>
</html>
